package com.feel.service.impl;

//import com.alibaba.fastjson.JSON;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.feel.mapper.PartMapper;
import com.feel.mapper.ServicePartsMapper;
import com.feel.pojo.Part;
import com.feel.pojo.ServiceParts;
import com.feel.service.ServicePartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 刘永钦
 * @Date: 2024-09-05-15:27
 * @Description:
 */
@Service
public class ServicePartsServiceImpl implements ServicePartsService {
    @Autowired
    private ServicePartsMapper servicePartsMapper;
    @Autowired
    private PartMapper partMapper;
    @Override
    public List<ServiceParts> getAllById(Integer id) {
        List<ServiceParts> list = servicePartsMapper.getByAppointmentId(id);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateInfo(Integer id, List<ServiceParts> parts) {

        List<ServiceParts> list = servicePartsMapper.getByAppointmentId(id);
        list.forEach(item -> {
            partMapper.useAddPart(item.getQuantity(), item.getPartId());
        });
        Integer i = servicePartsMapper.delInfo(id);

        if (parts.size() == 0) {
            return true;
        }
        //删除库存
        //使用jackson解决
        ObjectMapper mapper = new ObjectMapper();
        List<ServiceParts> parts1 = mapper.convertValue(parts, new TypeReference<List<ServiceParts>>() { });

        // 将LinkedHashMap对象进行转换，，使用fastjson解决，
        //List<ServiceParts> parts1 = JSON.parseArray(JSON.toJSONString(parts), ServiceParts.class);
        parts1.forEach( item -> {
            Integer i1 = partMapper.usePart(item.getQuantity(), item.getPartId());
        });
        Integer i1 = servicePartsMapper.addInfos(id, parts);
        return true;
    }

    @Override
    public List<ServiceParts> getAllParts() {
        List<Part> list = partMapper.getAll(null);
        List<ServiceParts> list1 = list.stream()
                .map(s -> {
                    ServiceParts serviceParts = new ServiceParts();
                    serviceParts.setPart(s);
                    serviceParts.setPartId(s.getPartId());
                    return serviceParts;
                })
                .collect(Collectors.toList());
        return list1;
    }

    @Override
    public Map<String, Object> getPartsEchartsInfo() {
        List<Map<String, Object>> mapList = servicePartsMapper.getPartsEchartsInfo();
        List<Object> number = mapList.stream()
                .map(item -> item.get("number"))
                .collect(Collectors.toList());
        List<String> name = mapList.stream()
                .map(item -> {
                    Part part = partMapper.getById((Integer) item.get("part_id"));
                    return part.getPartName();
                })
                .collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("number", number);
        return map;
    }
}
